<?php
namespace app\api\controller;

use app\api\model\Configs;
use app\api\model\GoodsCategory;


class Goods extends Base
{
    // 不需要验证
    protected $excludeValidateCheck = [

    ];

    // 定义自动实例化模型
    protected $ModelPath = 'Goods';

    public function detail()
    {
        //获取商品ID
        $id = (int)request()->param('id', 0);
        //查询商品信息
        if (!$goods_info = $this->M->field('*')->append(['images_format', 'unit_format', 'content_format'])->findOrEmpty($id)->toArray()) {
            //返回失败
            return showError('商品信息不存在');
        }
        //获取图册
        $images = $goods_info['images_format'];
        //判断信息
        if (empty($images)) {
            //追加内容
            array_unshift($images, $goods_info['cover']);
        }
        $is_open_buy = Configs::where('alias', 'IS_OPEN_SHOPPING')->value('content');;
        //整理基本结构
        $data = [
            'id' => $id,
            'title' => $goods_info['name'],
            'albums' => $images,
            'price' => $goods_info['sale_amount']/100,//number_format($goods_info['sale_amount'], 2),
            'cate_name' => GoodsCategory::where('id', (int)$goods_info['category_id'])->value('title'),
            'description' => (string)$goods_info['desc'] ?: $goods_info['name'],
            'tags' => [],
            'pictures' => $goods_info['content_format'] ?: ($goods_info['cover'] ? [$goods_info['cover']] : []),
            'is_open_buy'=>$is_open_buy == 'on' ? true : false,
        ];
        //返回数据
        return showSuccess($data);
    }


    public function categories(){
        //整理数据
        $data = [
            'tops'=>[],
            'categories'=>[],


            'title'=>'云上优品',
            'unread_message'=>'true',
            'search'=>[
                'enable'=>true,
                'placeholder'=>'',
                'scan'=>true,
                'alias'=>'none',
                'params'=>[]
            ],
            'banners'=>[
                'enable'=>true,
                'lists'=>[
                    [
                        'name'=>'',
                        'img'=>get_oss_url('/static/logos/ysyp.png'),
                        'alias'=>'none',
                        'params'=>[],
                    ],
                ]
            ],
            'ensures'=>[
                'enable'=>true,
                'lists'=>[]
            ],
            'navigations'=>[
                'enable'=>true,
                'lists'=>[
                    [
                        'id'=>0,
                        'name'=>'',
                        'logo'=>get_oss_url('/static/logos/ysyp.png'),
                        'alias'=>'none',
                        'params'=>[],
                    ],
                ]
            ],
            'notices'=>[
                'enable'=>true,
                'text'=>'',
                'alias'=>'none',
                'params'=>[]
            ],
            'thematic'=>[
                'enable'=>true,
                'left'=>[
                    [
                        'title'=>'',
                        'img'=>get_oss_url('/static/logos/ysyp.png'),
                        'alias'=>'none',
                        'params'=>[],
                        'lists'=>[
                            [
                                'code'=>'',
                                'cover'=>'',
                                'title'=>'',
                                'amount'=>0,
                                'market_amount'=>0,
                                'alias'=>'none',
                                'params'=>[],
                            ],
                        ]
                    ],
                ],
                'right'=>[],
            ],
            'activity'=>[
                'enable'=>true,
                'name'=>'',
                'img'=>get_oss_url('/static/logos/ysyp.png'),
                'alias'=>'none',
                'params'=>[],
            ],
            'brand'=>[
                'enable'=>true,
                'title'=>'',
                'more'=>'',
                'alias'=>'none',
                'params'=>[],
                'lists'=>[
                    [
                        'code'=>'',
                        'cover'=>'',
                        'title'=>'',
                        'amount'=>0,
                        'market_amount'=>0,
                        'alias'=>'none',
                        'params'=>[],
                    ],
                ]
            ],
            'recommend'=>[
                'enable'=>true,
                'title'=>'',
                'more'=>'',
                'alias'=>'none',
                'params'=>[],
                'lists'=>[
                    [
                        'code'=>'',
                        'cover'=>'',
                        'title'=>'',
                        'amount'=>0,
                        'market_amount'=>0,
                        'alias'=>'none',
                        'params'=>[],
                    ],
                ]
            ],
            'lists'=>[
                'enable'=>true,
                'title'=>'',
                'filters'=>[]
            ],
        ];

        return showSuccess($data);
    }

    //商品列表
    public function listsInformation(){
//        search: {enable: false, placeholder: '', button: ''},
//        switch: {enable: false, lists: [{text: '', alias: '', sort: false, filters: {}, sort_field: ''}]}
        //整理数据
        $data = [
            'search'=>[
                'enable'=>true,
                'placeholder'=>'',
                'button'=>'',
            ],
            'switch'=>[
                'enable'=>true,
                'lists'=>[
                    [
                        'text'=>'',
                        'alias'=>'',
                        'sort'=>true,
                        'filters'=>[],
                        'sort_field'=>'',
                    ]
                ],
            ],
        ];
        //响应数据
        return showSuccess($data);
    }
}